package com.leilei.controller;

import com.leilei.entity.CommodityColor;
import com.leilei.entity.CommodityImgs;
import com.leilei.service.IICommodityImgsService;
import com.leilei.service.impl.CommodityImgsServiceImpl;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

@WebServlet("/CommodityImgsServlet")
public class CommodityImgsServlet extends BaseServlet{

    private IICommodityImgsService commodityImgsService = new CommodityImgsServiceImpl();

    public CommodityImgsServlet(){

    }

    protected void saveCommodityImgs(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //	1.	创建DiskFileItemFactory对象
        DiskFileItemFactory dfif = new DiskFileItemFactory();

        //	2.	使用DiskFileItemFactory对象作为参数创建ServletFileUpload对象
        ServletFileUpload sfu = new ServletFileUpload(dfif);

        try {

            int pid = 0;
            int type = 0;
            //	借助ServletFileUpload对象的parseRequest(request)方法得到之前所有的表单元素
            List<FileItem> list = sfu.parseRequest(req);

            for (FileItem fi : list){
                if(fi.isFormField()) {
                    if ("ctid".equals(fi.getFieldName())){
                        type = Integer.parseInt(fi.getString());
                    }
                    if ("pid".equals(fi.getFieldName())){
                        pid = Integer.parseInt(fi.getString());
                    }
                }
            }

            for (FileItem fi : list) {
                if(!fi.isFormField()){					//	表单中的文件域

                    String path = "D:\\project\\shopping\\web\\imgs\\";
                    String filepage = null;

                    if (type == 1){
                        filepage = "phone/";
                    }else if (type == 2){
                        filepage = "home/";
                    }else {
                        filepage = "smart/";
                    }

                    path = path + filepage;

                    //	使用服务器路径创建一个File对象
                    File f = new File(path);

                    //	该路径不存在，则创建该路径
                    if(!f.exists()) {
                        f.mkdirs();
                    }

                    //	FileItem对象的getName（）拿到文件	全路径
                    String name = fi.getName();

                    //	对于文件全路径截取，得到单纯的文件名
                    name = name.substring(name.lastIndexOf(File.separator) + 1);

                    System.out.println(name);

                    //	使用之前的服务器路径外加文件名创建一个对象
                    File file = new File(path, name);

                    //	借助FileItem对象的write（f）方法把该文件实施上传功能
                    fi.write(file);

                    //新增图片
                    CommodityImgs commodityImgs = new CommodityImgs();
                    commodityImgs.setPid(pid);
                    commodityImgs.setSrc(filepage + name);
                    commodityImgs.setIsindex(1);

                    boolean result = commodityImgsService.insertCommodityImgs(commodityImgs);

                    if (result){
                        //跳转页面
                        req.getRequestDispatcher("/CommodityManagerServlet?method=getCommodityByPage&type=" + type).forward(req, resp);
                    } else{
                        //新增页面
                        req.getRequestDispatcher("/jsp/insertCommodityImgs.jsp?pid=" + pid + "&ctid=" + type).forward(req, resp);
                    }

                }
            }
        } catch (FileUploadException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

}
